package com.stagewebserver.webserver.controller;

import com.stagecommon.common.pojo.*;
import com.stagewebserver.webserver.service.WorkCommentService;
import com.stagewebserver.webserver.service.WorksInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author: yxl
 * @since: 2021-11-27
 **/
@Slf4j
@RestController
@RequestMapping("/worksInfo")
public class WorksInfoController {

    @Autowired
    private WorksInfoService worksInfoService;

    @Autowired
    private WorkCommentService workCommentService;

    /**
     * 获取新发布作品（按发布日期从新到旧排序）
     * @return
     */
    @GetMapping("/selectNewWorksInfo")
    public RespBean selectNewWorksInfo(){
        return RespBean.operationSuccess("查询成功", 200, worksInfoService.newWorksInfo());
    }

    /**
     * 获取热门作品（按点赞从多到少排序）
     * @return
     */
    @GetMapping("/selectHotWorksInfo")
    public RespBean selectHotWorksInfo(){
        return RespBean.operationSuccess("查询成功", 200, worksInfoService.hotWorksInfo());
    }

    /**
     * 根据作者获取作者的作品（按发布日期从新到旧排序）
     * @param user
     * @return
     */
    @PostMapping("/selectWorksInfoByUser")
    public RespBean selectWorksInfoByUser(@RequestBody User user){
        return RespBean.operationSuccess("查询成功",200,worksInfoService.userWorksInfo(user.getUserId()));
    }

    /**
     * 根据id获取作品信息
     * @param worksInfo
     * @return
     */
    @PostMapping("/getWorkInfoById")
    public RespBean<WorksInfo> getWorkInfoById(@RequestBody WorksInfo worksInfo){
        return worksInfoService.selectWorkInfoById(worksInfo.getWorkId());
    }

    /**
     * 关键字模糊查询作品
     * @param worksInfo
     * @param type
     * @return
     */
    @PostMapping("/getWorkInfoByKeyWord")
    public RespBean getWorkInfoByKeyWord(@RequestBody WorksInfo worksInfo,@RequestParam("type") String type){
        return RespBean.operationSuccess("搜索成功",200,worksInfoService.selectWorkByKeyword(worksInfo.getWorkName(),type));
    }

    /**
     * 根据作品id获取作品的评论
     * @param workComment
     * @return
     */
    @PostMapping("/getWorkComment")
    public RespBean getWorkComment(@RequestBody WorkComment workComment){
        return RespBean.operationSuccess("获取评论成功",200,workCommentService.listCommentByWorkId(workComment.getWorkId()));
    }

    /**
     * 给作品添加评论
     * @param workComment
     * @return
     */
    @PostMapping("/addWorkComment")
    public RespBean addWorkComment(@RequestBody WorkComment workComment){
        return RespBean.operationSuccess("评论成功",200,workCommentService.addComment(workComment));
    }

    /**
     * 根据分类获取作品
     * @param category
     * @param type
     * @return
     */
    @PostMapping("/searchWorksInfoByCategory")
    public RespBean<List<WorksInfo>> searchWorksInfoByCategory(@RequestBody Category category, @RequestParam("type") String type){
        return worksInfoService.selectWorksInfoByCategory(category,type);
    }

    /**
     * 根据作品id删除作品
     * @param worksInfo
     * @return
     */
    @PostMapping("/deleteWorkById")
    public RespBean<Integer> deleteWorkById(@RequestBody WorksInfo worksInfo){
        return worksInfoService.deleteWorkById(worksInfo);
    }

    /**
     * 上传作品文件
     * @param file
     * @param posterFile
     * @param worksInfo
     * @return
     * @throws Exception
     */
    @PostMapping("/uploadFile")
    public RespBean uploadFile(MultipartFile file, MultipartFile posterFile, WorksInfo worksInfo) throws Exception {
        return worksInfoService.uploadFile(file, posterFile, worksInfo);
    }



}
